<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use common\models\business\NewBusinessInspection;
\backend\assets\FullcalendarAsset::register($this);
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\business\models\NewBusinessInspectionSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '巡检管理';
$this->params['breadcrumbs'][] = $this->title;
?>


<section class="wrapper site-min-height">
    <!-- page start-->
    <section class="panel">
        <header class="panel-heading">
            <?= Html::a('返回','javascript:history.back(-1)'); ?>
            <?= Html::encode($this->title) ?>
        </header>
        <div class="panel-body">
            <div class="adv-table editable-table ">
 


<div class="new-business-inspection-index">



    <?php
    echo GridView::widget([
        'dataProvider'=>$dataProvider,
        'filterModel'=>$searchModel,
        'showPageSummary'=>true,
        'pjax'=>false,
        'striped'=>true,
        'hover'=>true,
        'panel'=>['type'=>'primary', 'heading'=>'巡检管理'],
        'containerOptions'=>['style'=>'overflow: auto'], // only set when $responsive = false
        'headerRowOptions'=>['class'=>'kartik-sheet-style'],
        'filterRowOptions'=>['class'=>'kartik-sheet-style'],
        'columns'=>[
            [
                'label' => '仔猪编号',
                'attribute'=>'pig.no',
                'format' => 'raw',
                'value' => function($model){
                    return $model->pig?Html::a($model->pig->no,\yii\helpers\Url::to(['/business/pig/view','id'=> $model->pig->id])):'';
                },
                'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                'width'=>'100px',
            ],
            [
                'attribute'=>'business_personid',
                'format' => 'raw',
                'value' => function($model){
                    return $model->person?Html::a($model->person->name,\yii\helpers\Url::to(['/business/person/view','id'=> $model->person->id])):'';
                },
                'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                'width'=>'100px',
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=>\yii\helpers\ArrayHelper::map(\common\models\business\BusinessPerson::find()->asArray()->all(),'id','name'),
                'filterWidgetOptions'=>[
                    'pluginOptions'=>['allowClear'=>true],
                ],
                'filterInputOptions'=>['placeholder'=>'业务员'],
            ],
            [
                'filterType'=>GridView::FILTER_DATE,
                'class' => '\kartik\grid\DataColumn',
                'filterWidgetOptions'=>[
                    'pluginOptions'=>['format' => 'yyyy-mm-dd'],
                ],
                'attribute' => 'createtime',
                'value'=>
                    function($model){
                        return  date('Y-m-d H:i:s',$model->createtime);   //主要通过此种方式实现
                    },
                'width'=>'150px',
            ],
            [
                'attribute'=>'status',
                'value' => function($model){
                    $str = '';
                    if($model->status==NewBusinessInspection::STATUS_PASSED) {
                        return Html::label(NewBusinessInspection::$statues[$model->status], '', ['class' => 'btn btn-xs btn-success']);
                    }else if($model->status==NewBusinessInspection::STATUS_UNPASSED) {

                        $str .= Html::label(NewBusinessInspection::$statues[$model->status], '', ['class' => 'btn btn-xs btn-danger','style'=>'margin-right:10px;']);
                        if($model->bujian){
                            if($model->bujian->status==1){
                                $str .= Html::label('已补审核通过','',['class' => 'btn btn-xs btn-success']);
                            }else if($model->bujian->status==-1){
                                $str .= Html::label('补检审核未通过','',['class' => 'btn btn-xs btn-danger']);
                            }if($model->bujian->status==0){
                                $str .= Html::label('补检未审核','',['class' => 'btn btn-xs btn-default']);
                            }
                        }
                        return $str;
                    }
                    return $str .= Html::label(NewBusinessInspection::$statues[$model->status], '', ['class' => 'btn btn-xs btn-default']);
                },
                'format' => 'raw',
                'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                'width'=>'100px',
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=>NewBusinessInspection::$statues,
                'filterWidgetOptions'=>[
                    'pluginOptions'=>['allowClear'=>true],
                ],
                'filterInputOptions'=>['placeholder'=>'是否通过审核'],
            ],
            [
                'attribute'=>'type',
                'value' => function($model){
                    return Html::label(NewBusinessInspection::$types[$model->type], '', ['class' => 'btn btn-xs btn-default']);

                },
                'format' => 'raw',
                'pageSummaryOptions'=>['class'=>'text-right text-warning'],
                'width'=>'100px',
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=>NewBusinessInspection::$types,
                'filterWidgetOptions'=>[
                    'pluginOptions'=>['allowClear'=>true],
                ],
                'filterInputOptions'=>['placeholder'=>'是否补检'],
            ],
            [
                'class' => '\kartik\grid\ActionColumn',
                'deleteOptions' => ['label' => '<i class="glyphicon glyphicon-remove"></i>'],
                'template' => '{view}{update}{delete}{dateshow}',
                'buttons' => [
                    'dateshow' => function($url,$model){
                        return "|<span class='calder' data-pigid='{$model->pigid}' data-type='no' data-toggle='modal' data-target='#calder-model'>巡检日历</span>";
                    }
                ]
            ],
            [
                'class' => '\kartik\grid\CheckboxColumn'
            ],
        ],
        'toolbar'=> [
            [
                'content' => Html::button('<i class="glyphicon glyphicon-delete">批量审核</i>', ['type'=>'button', 'title'=>'批量设置出栏时间', 'class'=>'btn btn-success', 'id'=>'setpass'])
            ],
            [
                'content' => Html::button('<i class="glyphicon glyphicon-delete">批量删除</i>', ['type'=>'button', 'title'=>'批量设置出栏时间', 'class'=>'btn btn-success', 'id'=>'mutidelete'])
            ],
            '{export}',
            '{toggleData}',
        ],
    ]);
    ?>



    <!--巡检日历弹窗-->
    <div id="calder-model" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="关闭"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="gridSystemModalLabel">巡检日历</h4>
                </div>
                <div class="modal-body" style="overflow: scroll;height: 700px;">
                    <div id='calendar'></div>
                    <input type="hidden" name="pigid" id="pigid" value="">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->


</div>
</div>
</div>
</section>
</section>



<script>
    <?php $this->beginBlock('js_end') ?>

    $('#setpass').click(function () {
        var keys = jqchk();
        if(confirm('确定要审核通过选中的巡检记录吗')){
            if(keys.length <= 0){
                alert('请选中对应的巡检记录');
                return false;
            }
            $.post('/business/new-business-inspection/setpass',{'ids':keys},function(res){
                console.log(res);
                alert(res.msg);
                if(res.status == 0)
                    window.location.href='/business/new-business-inspection/index';
            },'json');
        }
    });
    $('#mutidelete').click(function () {
        var keys = jqchk();
        if(confirm('确定要删除选中的巡检记录吗')){
            if(keys.length <= 0){
                alert('请选中对应的巡检记录');
                return false;
            }
            $.post('/business/new-business-inspection/muti-delete',{'ids':keys},function(res){
                console.log(res);
                alert(res.msg);
                if(res.status == 0)
                    window.location.href='/business/new-business-inspection/index';
            },'json');
        }
    });


    //巡检日历弹窗
    $('.calder').click(function(){
        $.get('/business/new-business-inspection/show-date',{pigid:$(this).data('pigid')},function(res){
            $('#pigid').val($(this).data('pigid'));
            $('#calder-model .modal-body').html(res);
            $('#calder-model').modal('show');
        })

    })



    function jqchk() { //jquery获取复选框值=
        var chk_value = [];
        $('input[name="selection[]"]:checked').each(function () {
            chk_value.push($(this).val());
        });
        return chk_value;
    }

    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>





